<?php
namespace common\services;

use common\models\SinglePage;
use yii\data\Pagination;
use yii\data\ActiveDataProvider;

class SinglePageService extends SinglePage
{

    /**
     * 查询单面内容
     * @param $code
     * @param bool $withCache
     * @return bool|mixed|null|static
     */
    public function getSinglePageByCode($code, $withCache = true) {
        $cacheKey = 'single_page_'.$code;
        $result = \Yii::$app->cache->get($cacheKey);
        if (empty($result)) {
            $result = SinglePage::find()->andFilterWhere(['code' => $code, 'status' => SinglePage::STATUS_FLAG_NORMAL])->asArray()->one();
            if (empty($result)) {
                return false;
            }
            if ($withCache) {
                \Yii::$app->cache->set($cacheKey, $result);
            }
        }
        return $result;
    }

}
